/*
  学习目标：Redux推荐写法
  1. 使用单独函数维护 reducer，一般命名 xxxReducer
  2. 初始值封装成对象，一般命名 initState
  3. switch 替代 if/else
    
*/
import { legacy_createStore as createStore } from 'redux';

// 2.初始值封装成对象
const initState = { count: 0 };
// 1. 使用单独函数维护 reducer，一般命名 xxxReducer
const countReducer = (state = initState, action) => {
  // 3. switch 替代 if/else
  switch (action.type) {
    case 'add':
      return { ...state, count: state.count + action.payload };
    case 'dec':
      return { ...state, count: state.count - action.payload };
    default:
      return state;
  }
};

const store = createStore(countReducer);

const unFn = store.subscribe(() => {
  console.log('state状态改变了 -----> ', store.getState().count);
});

store.dispatch({ type: 'add', payload: 1 });
store.dispatch({ type: 'add', payload: 10 });
store.dispatch({ type: 'dec', payload: 1 });
// 停止监听，后面在改变，不会再触发回调
unFn();
store.dispatch({ type: 'dec', payload: 10 });
// store.dispatch({ type: 'dec' });
// console.log('store.getState() -----> ', store.getState().count);

export default function App(params) {
  return <></>;
}
